package online.sh.cn.fy.users.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import online.sh.cn.fy.common.utils.CookieUtil;
import online.sh.cn.fy.common.utils.PackageUtil;
import online.sh.cn.fy.common.utils.TokenUtils;


/**
 * 发送绑定信息
 * 
 */
@WebServlet("/sendmobile")
public class SendMobileServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		TokenUtils to=new TokenUtils();
		String token=to.getshAccesstoken();
		String shcode=request.getParameter("code");
		String deviceno=CookieUtil.getCookieValueByName(request, "shmobile");
		System.out.println("绑定设备号:"+CookieUtil.decodeCookie(deviceno));
		if(null!=shcode && ""!=shcode){
			String openid=PackageUtil.getshOpenid(token, shcode);
			if (""!=openid && ""!=deviceno) {
				deviceno=CookieUtil.decodeCookie(deviceno);
				String result=PackageUtil.sendshMoblie(token, openid, deviceno);
				System.out.println("电信绑定："+result);
				response.sendRedirect("http://app.sh.189.cn/mwechat/query/package/use?channel=10033");
			}else{
				System.out.println("电信绑定出错---openid");
				response.sendRedirect("http://app.sh.189.cn/mwechat/query/package/use?channel=10033");
			}
		}else{
			System.out.println("电信绑定出错---shcode");
			response.sendRedirect("http://app.sh.189.cn/mwechat/query/package/use?channel=10033");
		}
	}

}
